package com.gxc.authcore.system.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxc.authcore.system.domain.Param;
import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.service.BeanService;
import com.gxc.dbcore.util.CompareItem;
import com.gxc.dbcore.util.CompareType;
import com.gxc.dbcore.util.ConditionUtil;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;

@Service
public class ParamService {

	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private BeanService beanService;

	public Param retrieve(String id) {
		return beanService.get(sqlDao, Param.class, id);
	}

	public int create(Param param) {
		return beanService.create(sqlDao, param);
	}

	public int update(Param param) {
		return beanService.update(sqlDao, param);
	}

	public int delete(String id) {
		return beanService.delete(sqlDao, Param.class, id);
	}

	public int delete(String[] idArray) {
		return beanService.delete(sqlDao, Param.class, idArray);
	}

	@SuppressWarnings("rawtypes")
	public List<CompareItem> buildConditions(Map params) {
		List<CompareItem> conditionList = new ArrayList<CompareItem>();
		conditionList.add(new CompareItem("id", CompareType.EQUAL));
		conditionList.add(new CompareItem("param_key", CompareType.LIKE));
		conditionList.add(new CompareItem("param_value", CompareType.LIKE));
		conditionList.add(new CompareItem("param_scope", CompareType.LIKE));
		conditionList = ConditionUtil.setConditionValue(conditionList, params);
		return conditionList;
	}

	@SuppressWarnings("rawtypes")
	public List list(Map params) {
		List<CompareItem> conditionList = this.buildConditions(params);
		String fieldSort = ConditionUtil.getFieldSort(params);
		return beanService.list(sqlDao, Param.class, conditionList, fieldSort);
	}

	@SuppressWarnings("rawtypes")
	public PageList listPage(Map params, PageInfo pageInfo) {
		List<CompareItem> coditionList = this.buildConditions(params);
		String fieldSort = ConditionUtil.getFieldSort(params);
		return beanService.listPage(sqlDao, Param.class, coditionList,
				fieldSort, pageInfo);
	}

}
